home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / lmath.s < prev    next >
Text File  |  1990-11-23  |  4KB  |  207 lines

  1. * Copyright (c) 1988 by Sozobon, Limited.  Author: Johann Ruegg
  2. *
  3. * Permission is granted to anyone to use this software for any purpose
  4. * on any computer system, and to redistribute it freely, with the
  5. * following restrictions:
  6. * 1) No charge may be made other than reasonable charges for reproduction.
  7. * 2) Modified versions must be clearly marked as such.
  8. * 3) The authors are not responsible for any harmful consequences
  9. *    of using this software, even if they result from defects in it.
  10. *
  11.     .text
  12.     .globl    ldiv
  13. ldiv:
  14.     move.l    4(a7),d0
  15.     bpl    ld1
  16.     neg.l    d0
  17. ld1:
  18.     move.l    8(a7),d1
  19.     bpl    ld2
  20.     neg.l    d1
  21.     eor.b    #$80,4(a7)
  22. ld2:
  23.     bsr    i_ldiv        /* d0 = d0/d1 */
  24.     tst.b    4(a7)
  25.     bpl    ld3
  26.     neg.l    d0
  27. ld3:
  28.     rts
  29.  
  30.     .globl    lmul
  31. lmul:
  32.     move.l    4(a7),d0
  33.     bpl    lm1
  34.     neg.l    d0
  35. lm1:
  36.     move.l    8(a7),d1
  37.     bpl    lm2
  38.     neg.l    d1
  39.     eor.b    #$80,4(a7)
  40. lm2:
  41.     bsr    i_lmul        /* d0 = d0*d1 */
  42.     tst.b    4(a7)
  43.     bpl    lm3
  44.     neg.l    d0
  45. lm3:
  46.     rts
  47.  
  48.     .globl    lrem
  49. lrem:
  50.     move.l    4(a7),d0
  51.     bpl    lr1
  52.     neg.l    d0
  53. lr1:
  54.     move.l    8(a7),d1
  55.     bpl    lr2
  56.     neg.l    d1
  57. lr2:
  58.     bsr    i_ldiv        /* d1 = d0%d1 */
  59.     move.l    d1,d0
  60.     tst.b    4(a7)
  61.     bpl    lr3
  62.     neg.l    d0
  63. lr3:
  64.     rts
  65.  
  66.     .globl    ldivu
  67. ldivu:
  68.     move.l    4(a7),d0
  69.     move.l    8(a7),d1
  70.     bsr    i_ldiv
  71.     rts
  72.  
  73.     .globl    lmulu
  74. lmulu:
  75.     move.l    4(a7),d0
  76.     move.l    8(a7),d1
  77.     bsr    i_lmul
  78.     rts
  79.  
  80.     .globl    lremu
  81. lremu:
  82.     move.l    4(a7),d0
  83.     move.l    8(a7),d1
  84.     bsr    i_ldiv
  85.     move.l    d1,d0
  86.     rts
  87.  
  88. * A in d0, B in d1, return A*B in d0
  89. i_lmul:
  90.     move.l    d3,a2        /* save d3 */
  91.     move.w    d1,d2
  92.     mulu    d0,d2        /* d2 = Al * Bl */
  93.  
  94.     move.l    d1,d3
  95.     swap    d3
  96.     mulu    d0,d3        /* d3 = Al * Bh */
  97.  
  98.     swap    d0
  99.     mulu    d1,d0        /* d0 = Ah * Bl */
  100.  
  101.     add.l    d3,d0        /* d0 = (Ah*Bl + Al*Bh) */
  102.     swap    d0
  103.     clr.w    d0        /* d0 = (Ah*Bl + Al*Bh) << 16 */
  104.  
  105.     add.l    d2,d0        /* d0 = A*B */
  106.     move.l    a2,d3        /* restore d3 */
  107.     rts
  108.  
  109. *A in d0, B in d1, return A/B in d0, A%B in d1
  110. i_ldiv:
  111.     tst.l    d1
  112.     bne    nz1
  113.  
  114. *    divide by zero
  115.     divu    #0,d0        /* cause trap */
  116.     move.l    #$80000000,d0
  117.     move.l    d0,d1
  118.     rts
  119. nz1:
  120.     move.l    d3,a2        /* save d3 */
  121.     cmp.l    d1,d0
  122.     bhi    norm
  123.     beq    is1
  124. *    A<B, so ret 0, rem A
  125.     move.l    d0,d1
  126.     clr.l    d0
  127.     move.l    a2,d3        /* restore d3 */
  128.     rts
  129. *    A==B, so ret 1, rem 0
  130. is1:
  131.     moveq.l    #1,d0
  132.     clr.l    d1
  133.     move.l    a2,d3        /* restore d3 */
  134.     rts
  135. *    A>B and B is not 0
  136. norm:
  137.     cmp.l    #1,d1
  138.     bne    not1
  139. *    B==1, so ret A, rem 0
  140.     clr.l    d1
  141.     move.l    a2,d3        /* restore d3 */
  142.     rts
  143. *  check for A short (implies B short also)
  144. not1:
  145.     cmp.l    #$ffff,d0
  146.     bhi    slow
  147. *  A short and B short -- use 'divu'
  148.     divu    d1,d0        /* d0 = REM:ANS */
  149.     swap    d0        /* d0 = ANS:REM */
  150.     clr.l    d1
  151.     move.w    d0,d1        /* d1 = REM */
  152.     clr.w    d0
  153.     swap    d0
  154.     move.l    a2,d3        /* restore d3 */
  155.     rts
  156. * check for B short
  157. slow:
  158.     cmp.l    #$ffff,d1
  159.     bhi    slower
  160. * A long and B short -- use special stuff from gnu
  161.     move.l    d0,d2
  162.     clr.w    d2
  163.     swap    d2
  164.     divu    d1,d2        /* d2 = REM:ANS of Ahi/B */
  165.     clr.l    d3
  166.     move.w    d2,d3        /* d3 = Ahi/B */
  167.     swap    d3
  168.  
  169.     move.w    d0,d2        /* d2 = REM << 16 + Alo */
  170.     divu    d1,d2        /* d2 = REM:ANS of stuff/B */
  171.  
  172.     move.l    d2,d1
  173.     clr.w    d1
  174.     swap    d1        /* d1 = REM */
  175.  
  176.     clr.l    d0
  177.     move.w    d2,d0
  178.     add.l    d3,d0        /* d0 = ANS */
  179.     move.l    a2,d3        /* restore d3 */
  180.     rts
  181. *    A>B, B > 1
  182. slower:
  183.     move.l    #1,d2
  184.     clr.l    d3
  185. moreadj:
  186.     cmp.l    d0,d1
  187.     bhs    adj
  188.     add.l    d2,d2
  189.     add.l    d1,d1
  190.     bpl    moreadj
  191. * we shifted B until its >A or sign bit set
  192. * we shifted #1 (d2) along with it
  193. adj:
  194.     cmp.l    d0,d1
  195.     bhi    ltuns
  196.     or.l    d2,d3
  197.     sub.l    d1,d0
  198. ltuns:
  199.     lsr.l    #1,d1
  200.     lsr.l    #1,d2
  201.     bne    adj
  202. * d3=answer, d0=rem
  203.     move.l    d0,d1
  204.     move.l    d3,d0
  205.     move.l    a2,d3        /* restore d3 */
  206.     rts
  207.